package com.junlt.p2p.ui.activity;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.junlt.p2p.R;
import com.junlt.p2p.entity.BaseResultEntity;
import com.junlt.p2p.entity.InvestListBean;
import com.junlt.p2p.httpmanage.simplehttp.HttpOnNextListener;
import com.junlt.p2p.ui.base.BaseRxAppCompatActivity;

import java.util.List;

import butterknife.BindView;

public class MainActivity extends BaseRxAppCompatActivity  {


    @BindView(R.id.message)
    TextView message;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
//    @BindView(R.id.error_layout)
//    LoadStateLayout errorLayout;
    private TextView mTextMessage;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        message.setText("改变");
        setSwipeBackEnable(false);
        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        toolbar.setTitle("这是主页");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                supportFinishAfterTransition();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_share, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_share:
                //普通请求
//                SubjectPostApi postEntity = new SubjectPostApi(simpleOnNextListener, this);
//                HttpManager manager = HttpManager.getInstance();
//                manager.doHttpDeal(postEntity);
                //文件上传

                break;
            case android.R.id.home:
                onBackPressed();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    //   回调一一对应
    HttpOnNextListener simpleOnNextListener = new HttpOnNextListener<List<InvestListBean>>() {
        @Override
        public void onNext(List<InvestListBean> investListBeans) {
            message.setText("缓存返回：\n" + investListBeans.toString());
        }

        @Override
        public void onCacheNext(String cache) {
            /*缓存回调*/
            Gson gson = new Gson();
//            java.lang.reflect.Type type = new TypeToken<BaseResultEntity<List<SubjectResulte>>>() {}.getType();
            BaseResultEntity resultEntity = gson.fromJson(cache, BaseResultEntity.class);
            message.setText("缓存返回：\n" + resultEntity.getData().toString());
        }

        /*用户主动调用，默认是不需要覆写该方法*/
        @Override
        public void onError(Throwable e) {
            super.onError(e);
            message.setText("失败：\n" + e.toString());
        }

        /*用户主动调用，默认是不需要覆写该方法*/
        @Override
        public void onCancel() {
            super.onCancel();
            message.setText("取消請求");
        }
    };

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    //singletask模式不走oncreate
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }



        private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
                = new BottomNavigationView.OnNavigationItemSelectedListener() {

            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.navigation_home:
                        mTextMessage.setText(R.string.title_home);
                        return true;
                    case R.id.navigation_dashboard:
                        mTextMessage.setText(R.string.title_dashboard);
                        return true;
                    case R.id.navigation_notifications:
                        mTextMessage.setText(R.string.title_notifications);
                        return true;
                }
                return false;
            }
        };

    }
